package bt;

import javax.microedition.lcdui.Choice;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.List;

public class ClientServerSelect  extends List implements CommandListener   {

    
     
        /**
         * Constructor
         */
        public ClientServerSelect()
        {
            super( "Select Role:", List.IMPLICIT );

            
            // Set text wrap around
            setFitPolicy( Choice.TEXT_WRAP_ON );
            // Create List
            append( "Server", null );
            append( "Client (FIRST_FOUND)", null );
            append( "Client (ALL_FOUND)", null );
            append( "Client (SELECT_ONE)", null );
            append( "Client (SELECT_SEVERAL)", null );
            // append commands
            addCommand( new Command( "Select", Command.OK, 1 ) );
            addCommand( new Command( "Quit", Command.EXIT, 1 ) );
            setCommandListener( this );
        }

   

        /** Starts server or client (or cancels/terminate the midlet).
         * @param c The command.
         * @param d The displayable object. */
        public void commandAction( Command c, Displayable d )
        {
            if( c.equals(List.SELECT_COMMAND) || (c.getCommandType() == Command.OK) )
            {
                int i = getSelectedIndex();
                String s = getString( i );
                if( s.equals( "Server" ) )
                {
                    // Start Server
                    ServerThread st = new ServerThread();
                    st.start();
                }
                if( s.equals( "Client (FIRST_FOUND)" ) )
                {   // Start Client with SEARCH_CONNECT_FIRST_FOUND
                    ClientThread ct = new ClientThread( BluetoothDiscovery.SEARCH_CONNECT_FIRST_FOUND );
                    ct.start();
                }
                if( s.equals( "Client (ALL_FOUND)" ) )
                {   // Start Client with SEARCH_CONNECT_ALL_FOUND
                    ClientThread ct = new ClientThread( BluetoothDiscovery.SEARCH_CONNECT_ALL_FOUND );
                    ct.start();
                }
                if( s.equals( "Client (SELECT_ONE)" ) )
                {   // Start Client with SEARCH_ALL_DEVICES_SELECT_ONE
                    ClientThread ct = new ClientThread( BluetoothDiscovery.SEARCH_ALL_DEVICES_SELECT_ONE );
                    ct.start();
                }
                if( s.equals( "Client (SELECT_SEVERAL)" ) )
                {   // Start Client with SEARCH_ALL_DEVICES_SELECT_SEVERAL
                    ClientThread ct = new ClientThread( BluetoothDiscovery.SEARCH_ALL_DEVICES_SELECT_SEVERAL );
                    ct.start();
                }
            }
            else if( c.getCommandType() == Command.EXIT )
            {
                // pass through to midlet
                //Exit();
            }
        }
    }